package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty((String)params.get(""))){
            queryWrapper.eq("brand_id",params.get("key"));
            IPage<CategoryBrandRelationEntity> page = this.page(
                    new Query<CategoryBrandRelationEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }else {
            IPage<CategoryBrandRelationEntity> page = this.page(
                    new Query<CategoryBrandRelationEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public void updateCategory(Long catelogId, String catelogName) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setCatelogId(catelogId);
        relationEntity.setCatelogName(catelogName);
        //this.update(relationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("cate_log_id",attrId));
        this.baseMapper.updateCategory(catelogId,catelogName);
    }

    @Override
    public List<BrandVo> brandList(Long catId) {
        List<CategoryBrandRelationEntity> selectList = this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        if (!selectList.isEmpty()){
            List<BrandVo> collect = selectList.stream().map((item) -> {
                //品牌名称修改的时候，会同步修改关联表里的品牌信息，因此不需要在查询品牌表了
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(item.getBrandId());
                brandVo.setBrandName(item.getBrandName());
                return brandVo;
            }).collect(Collectors.toList());
            return collect;
        }

       return new ArrayList<BrandVo>();
    }
}